<?php

declare(strict_types=1);

namespace App\Repository;
/**
 * 收藏文件夹数据访问层
 * 作者 刘传望
 * 日期 2025-10-17
 */
use App\Model\FavoriteFolder;
use Hyperf\Di\Annotation\Inject;

class FavoriteFolderRepository
{
    #[Inject]
    private FavoriteFolder $favoriteFolderModel;

    /**
     * 获取用户的收藏夹列表
     * @param int $userId
     * @return array
     */
    public function getUserFolders(int $userId): array
    {
        $folders = $this->favoriteFolderModel->query()
            ->where('user_id', $userId)
            ->withCount('favorites')
            ->orderBy('created_at', 'desc')
            ->get();

        return $folders->toArray();
    }

    /**
     * 根据用户ID和名称查找收藏夹
     * @param int $userId
     * @param string $name
     * @return FavoriteFolder|null
     */
    public function findByName(int $userId, string $name): ?FavoriteFolder
    {
        return $this->favoriteFolderModel->query()
            ->where('user_id', $userId)
            ->where('name', $name)
            ->first();
    }

    /**
     * 根据ID和用户ID查找收藏夹
     * @param int $folderId
     * @param int $userId
     * @return FavoriteFolder|null
     */
    public function findByIdAndUser(int $folderId, int $userId): ?FavoriteFolder
    {
        return $this->favoriteFolderModel->query()
            ->where('id', $folderId)
            ->where('user_id', $userId)
            ->first();
    }

    /**
     * 创建收藏夹
     * @param array $data
     * @return FavoriteFolder
     */
    public function create(array $data): FavoriteFolder
    {
        return $this->favoriteFolderModel->create($data);
    }

    /**
     * 更新收藏夹
     * @param FavoriteFolder $folder
     * @param array $data
     * @return FavoriteFolder
     */
    public function update(FavoriteFolder $folder, array $data): FavoriteFolder
    {
        $folder->update($data);
        return $folder->fresh();
    }

    /**
     * 删除收藏夹
     * @param FavoriteFolder $folder
     * @return bool
     */
    public function delete(FavoriteFolder $folder): bool
    {
        return $folder->delete();
    }

    /**
     * 检查用户收藏夹名称是否重复（排除指定ID）
     * @param int $userId
     * @param string $name
     * @param int|null $excludeId
     * @return bool
     */
    public function isNameExists(int $userId, string $name, ?int $excludeId = null): bool
    {
        $query = $this->favoriteFolderModel->query()
            ->where('user_id', $userId)
            ->where('name', $name);

        if ($excludeId) {
            $query->where('id', '!=', $excludeId);
        }

        return $query->exists();
    }

    /**
     * 获取可访问的收藏夹（用户自己的或公开的）
     * @param int $folderId
     * @param int $userId
     * @return FavoriteFolder|null
     */
    public function findAccessibleFolder(int $folderId, int $userId): ?FavoriteFolder
    {
        return $this->favoriteFolderModel->query()
            ->where('id', $folderId)
            ->where(function($query) use ($userId) {
                $query->where('user_id', $userId)
                      ->orWhere('is_public', true);
            })
            ->first();
    }
}
